package com.example.day7.View;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day7.Model.Bean;
import com.example.day7.Model.MyReceiver;
import com.example.day7.Presener.NetPresener;
import com.example.day7.R;

import java.util.ArrayList;

public class FragmentA extends Fragment {

    private View view;
    private RecyclerView recyclerView;
    private RAdapter adapter;
    private ArrayList<Bean.DataBean.DatasBean> list;
    private MyReceiver myReceiver;
    private IntentFilter intentFilter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = LayoutInflater.from(getContext()).inflate(R.layout.layout_fragment_a, null);
        recyclerView = view.findViewById(R.id.fragment_a_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        list = new ArrayList<>();

        adapter = new RAdapter(getContext(), list);
        recyclerView.setAdapter(adapter);

        initData();


        adapter.setOnItemClickListener(new RAdapter.OnItemClickListener() {
            @Override
            public Void OnItemClick(final int Position) {

                NetPresener netPresener = new NetPresener();
                netPresener.initData(new NetView() {
                    @Override
                    public void getData(Bean bean) {

                        Intent intent = new Intent();
                        intent.setAction("com.example.day7");
                        intent.putExtra("title", bean.getData().getDatas().get(Position).getTitle());
                        intent.putExtra("pic", bean.getData().getDatas().get(Position).getEnvelopePic());

                        getActivity().sendBroadcast(intent);

                    }
                }, page);


                return null;
            }
        });


        return view;
    }

    int page = 1;

    private void initData() {

        NetPresener netPresener = new NetPresener();

        netPresener.initData(new NetView() {
            @Override
            public void getData(Bean bean) {
                adapter.addData(bean.getData().getDatas());
            }
        }, page);


    }

    @Override
    public void onResume() {
        super.onResume();

        myReceiver = new MyReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.day7");
        getActivity().registerReceiver(myReceiver, intentFilter);

    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(myReceiver);
    }


}